package cz.cvut.fel.gfupan.gui.execution;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.text.JTextComponent;

public class Average implements Executable {
    
    private List<JTextComponent> list;
    
    public Average(JTextComponent... component) {
        list = new ArrayList<JTextComponent>();
        
        if (component != null && component.length > 0) {
            list.addAll(Arrays.asList(component));
        }
    }
    
    @Override
    public String execute() {
        BigInteger result = BigInteger.ZERO;
        int counter = 0;
        for (JTextComponent txt : list) {
            try {
                result = result.add(new BigInteger(txt.getText()));
                counter++;
            } catch (NumberFormatException e) {
            }
        }
        return counter == 0 ? "" : result.divide(BigInteger.valueOf(counter)).toString();
    }
}
